﻿using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows;
using System;
using System.Windows.Input;

namespace JonThomas.SpaceBounce
{
	public partial class Page : UserControl
	{
		private SpaceShip _spaceShip;
		private Storyboard _gameLoop = new Storyboard();
		private bool _isPaused = true;
		private int _screenWidth;
		private int _screenHeight;
		private static double _maxSpeed;
		/// <summary>Milliseconds between each time the screen is refreshed</summary>
		private int _refreshRate;

		public Page()
		{
			InitializeComponent();

			this.Loaded += new RoutedEventHandler(Page_Loaded);
			this.KeyDown += new KeyEventHandler(Page_KeyDown);
			this.KeyUp += new KeyEventHandler(Page_KeyUp);
			this.MouseLeftButtonDown += new MouseButtonEventHandler(Page_MouseLeftButtonDown);
			this.LostFocus += new RoutedEventHandler(Page_LostFocus);

			_refreshRate = 0;

			txtDebug1.Text = txtDebug2.Text = txtDebug3.Text = string.Empty;
		}

		void Page_Loaded(object sender, RoutedEventArgs e)
		{
			clickToStart.Text = "Click to Start!";
			_gameLoop.Duration = TimeSpan.FromMilliseconds(_refreshRate);
			_gameLoop.Completed += new EventHandler(MainGameLoop);
		}

		void Page_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			if (_isPaused)
			{
				_screenHeight = (int)_myCanvas.ActualHeight;
				_screenWidth = (int)_myCanvas.ActualWidth;

				clickToStart.Visibility = Visibility.Collapsed;
				CreateSprite();
				this.GotFocus += new RoutedEventHandler(Page_GotFocus);

				_isPaused = false;
				_gameLoop.Begin();
			}
		}

		void Page_LostFocus(object sender, RoutedEventArgs e)
		{
			clickToStart.Text = "Click to continue";
			clickToStart.Visibility = Visibility.Visible;
			_isPaused = true;
		}

		void Page_GotFocus(object sender, RoutedEventArgs e)
		{
			clickToStart.Visibility = Visibility.Collapsed;
			_isPaused = false;
			MainGameLoop(null, null);
		}

		#region Key handlers

		void Page_KeyDown(object sender, KeyEventArgs e)
		{
			switch(e.Key)
			{
				case Key.Left:
					_spaceShip.StartLeftTurn();
					break;
				case Key.Right:
					_spaceShip.StartRightTurn();
					break;
				case Key.Up:
					_spaceShip.StartAcceleration();
					break;
			}
		}

		void Page_KeyUp(object sender, KeyEventArgs e)
		{
			switch(e.Key)
			{
				case Key.Left:
				case Key.Right:
					_spaceShip.StopTurning();
					break;
				case Key.Up:
					_spaceShip.StopAcceleration();
					break;
			}
		}

		#endregion

		private void CleanUp()
		{
			_myCanvas.Children.Remove(_spaceShip);
		}

		private void CreateSprite()
		{
			lock (this)
			{
				CleanUp();

				#region TriangleShip
				//int imageWidth = 33;
				//int imageHeight = 41;
				#endregion

				#region FighterShip
				int imageWidth = 32;
				int imageHeight = 24;
				#endregion

				_spaceShip = new SpaceShip(new Size(imageWidth, imageHeight), new Size(_screenWidth, _screenHeight)
					, new Point(100, 100), _refreshRate == 0 ? 10 : _refreshRate/5);
				_spaceShip.SetImage("images/FighterShip.png");
				_myCanvas.Children.Add(_spaceShip);
			}
		}

		private void MoveSprite()
		{
			_spaceShip.Move();
		}

		private void MainGameLoop(object sender, EventArgs e)
		{
			if (!_isPaused)
			{
				MoveSprite();
				PrintDebug();
				_gameLoop.Begin();
			}
		}

		private void PrintDebug()
		{
			txtDebug1.Text = "X=" + (int)_spaceShip.PosX;
			txtDebug2.Text = "Y=" + (int)_spaceShip.PosY;
			if (_spaceShip.Vector.Speed > _maxSpeed)
				_maxSpeed = _spaceShip.Vector.Speed;
			txtDebug3.Text = string.Format("MaxSpeed={2}, Angle={0} ({1})", _spaceShip.AngleDegrees, _spaceShip.AngleRadians, _maxSpeed);
		}
	}
}
